package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class DispatchHandler {


    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 每天凌晨0点启动任务，远程调用用户服务更新VIP会员标识
     */
    @XxlJob("updateVipStatus")
    public void updateVipStatus() {
        log.info("[调度服务]更新VIP会员标识任务执行.");
        userFeignClient.updateVipExpireStatus();
    }


    @Autowired
    private SearchFeignClient searchFeignClient;


    /**
     * 每隔2小时启动任务，远程调用搜索服务更新Redis中不同分类下TOP10数据
     */
    @XxlJob("updateRedisRanking")
    public void updateRedisRanking() {
        log.info("[调度服务]更新Redis分类排行TOP10数据.");
        searchFeignClient.updateLatelyAlbumRanking();
    }


    /**
     * 测试任务
     * 1.任务参数
     */
    @XxlJob("myJob")
    public void myJOb() {
        String jobParam = XxlJobHelper.getJobParam();
        log.info("myJob...,任务参数：{}", jobParam);
        if (jobParam.equals("a")) {
            throw new RuntimeException("任务异常来了。。。");
        }

        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        log.info("总分片数：{}， 当前分片索引：{}", shardTotal, shardIndex);
    }
}
